package com.cn.icss.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.icss.service.ICommentService;
import com.cn.icss.service.impl.CommentServiceImpl;
import com.cn.icss.validator.CommentValidator;
import com.cn.icss.vo.CommentInfo;
import com.cn.icss.vo.common.ResponseCode;
import com.cn.icss.vo.common.ResponseResult;
import com.cn.icss.vo.common.ResponseStatus;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Page;
import live.autu.plugin.jfinal.swagger.annotation.Api;
import live.autu.plugin.jfinal.swagger.annotation.ApiImplicitParam;
import live.autu.plugin.jfinal.swagger.annotation.ApiImplicitParams;
import live.autu.plugin.jfinal.swagger.annotation.ApiOperation;
import live.autu.plugin.jfinal.swagger.config.RequestMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.UUID;

/**
 * @中软华腾软件系统有限公司,未经授权不得擅自传播
 * @类描述:
 * @类名称:CommentController
 * @作者: Administrator
 * @编辑时间: 2020/05/12 15:31
 * @版本: 1.0
 */
@Api(description = "评论接口",hidden = true)
public class CommentController extends Controller {

    Logger logger = Logger.getLogger(CommentController.class);

    public static final int PAGE_DEFAULT = 1;
    public static final int LIMIT_DEFAULT = 10; // 默认每页显示条数


    @Inject(CommentServiceImpl.class)
    private ICommentService commentService;


    @ApiOperation(methods= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="targetId",required=true, description="评论对象id(小视频id)"),
            @ApiImplicitParam(name="userId",required=true, description="用户Id"),
            @ApiImplicitParam(name="userName",required=true, description="用户名"),
            @ApiImplicitParam(name="content",required=true, description="评论内容")
    })
    @Before({POST.class, CommentValidator.class})
    public void saveComment(){
        // 验证通过正常开发逻辑
        String targetId = getPara("targetId");
        String userId = getPara("userId");
        String userName = getPara("userName");
        String content = getPara("content");
        String id = UUID.randomUUID().toString();
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setCreateTime(new Date());
        commentInfo.setTargetId(targetId);
        commentInfo.setByUserId(userId);
        commentInfo.setByUserName(userName);
        commentInfo.setContent(content);
        commentInfo.setId(id);
        boolean result = this.commentService.saveComment(commentInfo);
        ResponseResult responseResult = null;
        if(result){
            responseResult = ResponseResult.build(ResponseCode.OK, "保存成功", ResponseStatus.SUCCESS,result);
        }else{
            responseResult = ResponseResult.build(ResponseCode.Error, "保存失败", ResponseStatus.FAIL);
        }
        renderJson(JSONObject.toJSONString(responseResult));
    }


    @ApiOperation(methods= RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="targetId",required=true, description="评论对象id(小视频id)"),
            @ApiImplicitParam(name="pageSize",required=false, description="每页显示数据条数"),
            @ApiImplicitParam(name="pageNum",required=false, description="第几页")
    })
    @Before({POST.class, CommentValidator.class})
    public void findCommentSplit(){
        int page = PAGE_DEFAULT;
        int limit = LIMIT_DEFAULT;
        String targetId = super.getPara("targetId");
        if(StringUtils.isNotBlank(super.getPara("pageNum"))){
                page = Integer.parseInt(super.getPara("pageNum"));
        }
        if(StringUtils.isNotBlank(super.getPara("pageSize"))){
                limit = Integer.parseInt(super.getPara("pageSize"));
        }
        Page<CommentInfo> commentInfoPage = commentService.findCommentSplit(targetId, page, limit);
        ResponseResult responseResult = ResponseResult.ok(commentInfoPage);
        renderJson(JSONObject.toJSONString(responseResult));
    }


}
